//
//  LZFPublishVC.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/7/7.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFPublishVC.h"
#import "LZFPublishButton.h"
#import <POP.h>
#import "LZFWordPostController.h"
#import "LZFNavgationController.h"

@interface LZFPublishVC ()
@property(nonatomic,strong)NSArray *imageArray;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *timeArray;

@property(nonatomic,weak)UIImageView * sloganImageView;

/** 按钮数组 */
@property(nonatomic,strong)NSMutableArray *buttonArray;



@end

@implementation LZFPublishVC

#pragma mark 懒加载数据源
-(NSArray *)imageArray
{
    if (_imageArray==nil) {
        _imageArray=@[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];
    }
    return  _imageArray;
}

-(NSArray *)titleArray{

    if (_titleArray==nil) {
        _titleArray=@[@"发视频",@"发图片",@"发段子",@"发声音",@"审帖",@"离线下载"];
    }
    return _titleArray;
}


-(NSArray *)timeArray
{
    double interval=0.1;
    if (_timeArray==nil) {
        _timeArray=@[@(6*interval),@(3*interval),@(interval),@(2*interval),@(5*interval),@(4*interval),@(7*interval)];
    }
    return _timeArray;
}

-(UIImageView *)sloganImageView
{
    if (_sloganImageView==nil) {
        UIImageView *sloganImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
         _sloganImageView=sloganImageView;
        _sloganImageView.xmg_centerX=screenW*0.5;
        _sloganImageView.xmg_centerY=-screenH*0.2;
        [self.view addSubview:_sloganImageView];

    }
    return _sloganImageView;
}

-(NSMutableArray *)buttonArray
{
    if (_buttonArray==nil) {
        _buttonArray=[NSMutableArray new];
    }
    return _buttonArray;
}

#pragma mark viewDidload
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置一开始,控制器的view不允许和交互
    self.view.userInteractionEnabled=NO;
    
    //设置中间按钮
    [self setUpButton];
    
    //设置sloganImageView
   [self setUpSloganImageView];
    
   
    
}


    //设置中间按钮
-(void)setUpButton
{
    
    //const
    NSInteger buttonCount=6;
    NSInteger cols=3;
    
    //button frame
    CGFloat buttonW=screenW/cols;
    CGFloat buttonH=buttonW*0.8;
    CGFloat buttonX=0;
    CGFloat buttonY=0;
    CGFloat space = (screenH-2*buttonH)*0.5;
    
    //循环添加按钮
    for (int i=0; i<buttonCount; i++) {
        //创建 添加 frame 内容
        LZFPublishButton  *button=[LZFPublishButton buttonWithType:UIButtonTypeCustom];
        button.tag=i;
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:button];
        [self.view addSubview:button];
        buttonX=(i%cols)*buttonW;
        buttonY=(i/cols)*buttonH-screenH;
        //buttonY=space + (i/cols)*buttonH;
        button.frame=CGRectMake(buttonX, buttonY, buttonW, buttonH);
        [button setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        
        //动画 ,对frame作动画
        POPSpringAnimation   *springAnima=[POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        //springAnima.fromValue=[NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
        springAnima.toValue=[NSValue valueWithCGRect:CGRectMake(buttonX, space + (i/cols)*buttonH, buttonW, buttonH)];
        springAnima.springBounciness=10;
        springAnima.springSpeed=10;
        springAnima.beginTime=CACurrentMediaTime()+[self.timeArray[i] doubleValue];
        [button pop_addAnimation:springAnima forKey:nil];
}

}


//设置sloganImageView
-(void)setUpSloganImageView
{
    //动画 ,对Layer做动画
    POPSpringAnimation  *springAnima=[POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    springAnima.toValue=@(screenH*0.2);
    springAnima.springBounciness=10;
    springAnima.springSpeed=10;
    springAnima.beginTime=CACurrentMediaTime()+ [[self.timeArray lastObject] doubleValue];
 
    WeakSelf

    [springAnima setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        //sloganImageView动画完成后,允许用户交互,注意block的循环引用
         weakSelf.view.userInteractionEnabled=YES;
        
    }];
    [self.sloganImageView.layer pop_addAnimation:springAnima forKey:nil];

}


#pragma  mark 点击取消按钮
- (IBAction)clickCancle:(void(^)())mask{
    
    //动画过程中禁止用户交互
    self.view.userInteractionEnabled=NO;
    
    //button 退场动画
    for (int i=0; i<self.buttonArray.count; i++) {
        
        LZFPublishButton *button=self.buttonArray[i];
        //对每个button做动画
        POPSpringAnimation   *springAnima=[POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        springAnima.toValue=[NSValue valueWithCGRect:CGRectMake(button.xmg_x , button.xmg_y+screenH, button.xmg_width, button.xmg_height)];
        springAnima.springBounciness=10;
        springAnima.springSpeed=10;
        springAnima.beginTime=CACurrentMediaTime()+[self.timeArray[i] doubleValue];
        [button pop_addAnimation:springAnima forKey:nil];

    }
    
    //slognview退场
    
    //动画 ,对Layer做动画
    POPSpringAnimation  *springAnima=[POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    springAnima.toValue=@(self.sloganImageView.xmg_y+screenH);
    springAnima.springBounciness=10;
    springAnima.springSpeed=10;
    springAnima.beginTime=CACurrentMediaTime()+ [[self.timeArray lastObject] doubleValue];
    
    WeakSelf
    
    [springAnima setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        
        //sloganImageView动画完成后,允许用户交互,注意block的循环引用
        weakSelf.view.userInteractionEnabled=YES;
        
        //modal出来的控制器退场
        [self dismissViewControllerAnimated:NO completion:nil];
        
        //如果传过来的block有值那么就执行
        if (mask) {
            mask();
            
        }
   
    }];
    [self.sloganImageView.layer pop_addAnimation:springAnima forKey:nil];
    
    
}

#pragma  mark 监听用户点击屏幕
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //相当于点击了一下取消按钮
    [self clickCancle:nil];
}

#pragma mark -点击按钮响应
-(void)clickButton:(UIButton *)button
{
    //点击按钮实现效果 有一部分是点击取消按钮一样的效果,额外要做的事情,通过Block传递进去就好了
    [self clickCancle:^{
        //根据点击的button,作出不同的响应
        switch (button.tag) {

            case 2:
                 {
                     //modal出wordPostVC
                     LZFWordPostController *wordPostVC=[[LZFWordPostController alloc]init];

                     LZFNavgationController *nav=[[LZFNavgationController alloc]initWithRootViewController:wordPostVC];
                     
                     [[UIApplication sharedApplication].keyWindow.rootViewController presentModalViewController:nav animated:YES];
                     
                     break;

                  }
            
            case 0:
                LZFLog(@"发视频");
                break;
            case 1:
                 LZFLog(@"发图片");
                break;
            case 3:
                 LZFLog(@"发声音");
                break;
 
            case 4:
                  LZFLog(@"审帖");
                break;
            
            case 5:
                 LZFLog(@"离线下载");
                break;
            default:
                 LZFLog(@"出错了");
                break;
        }
    }];
}


@end
